/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.biz.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.biz.dao.BizOrderChildDao;
import com.jeesite.modules.biz.dao.BizOrderDao;
import com.jeesite.modules.biz.entity.BizOrder;
import com.jeesite.modules.biz.entity.BizOrderChild;
import com.jeesite.modules.file.utils.FileUploadUtils;

/**
 * 订单表Service
 * @author Lai_Hanzhang
 * @version 2019-03-13
 */
@Service
@Transactional(readOnly=true)
public class BizOrderService extends CrudService<BizOrderDao, BizOrder> {
	
	@Autowired
	private BizOrderChildDao bizOrderChildDao;
	
	/**
	 * 获取单条数据
	 * @param bizOrder
	 * @return
	 */
	@Override
	public BizOrder get(BizOrder bizOrder) {
		BizOrder entity = super.get(bizOrder);
		if (entity != null){
			BizOrderChild bizOrderChild = new BizOrderChild(entity);
			bizOrderChild.setStatus(BizOrderChild.STATUS_NORMAL);
			entity.setBizOrderChildList(bizOrderChildDao.findList(bizOrderChild));
		}
		return entity;
	}
	
	/**
	 * 查询分页数据
	 * @param bizOrder 查询条件
	 * @param bizOrder.page 分页对象
	 * @return
	 */
	@Override
	public Page<BizOrder> findPage(BizOrder bizOrder) {
		return super.findPage(bizOrder);
	}
	
	/**
	 * 保存数据（插入或更新）
	 * @param bizOrder
	 */
	@Override
	@Transactional(readOnly=false)
	public void save(BizOrder bizOrder) {
		super.save(bizOrder);
		// 保存上传附件
		FileUploadUtils.saveFileUpload(bizOrder.getId(), "bizOrder_file");
		// 保存 BizOrder子表
		for (BizOrderChild bizOrderChild : bizOrder.getBizOrderChildList()){
			if (!BizOrderChild.STATUS_DELETE.equals(bizOrderChild.getStatus())){
				bizOrderChild.setParent(get(bizOrder));
				if (bizOrderChild.getIsNewRecord()){
					bizOrderChild.preInsert();
					bizOrderChildDao.insert(bizOrderChild);
				}else{
					bizOrderChild.preUpdate();
					bizOrderChildDao.update(bizOrderChild);
				}
			}else{
				bizOrderChildDao.delete(bizOrderChild);
			}
		}
	}
	
	/**
	 * 更新状态
	 * @param bizOrder
	 */
	@Override
	@Transactional(readOnly=false)
	public void updateStatus(BizOrder bizOrder) {
		super.updateStatus(bizOrder);
	}
	
	/**
	 * 删除数据
	 * @param bizOrder
	 */
	@Override
	@Transactional(readOnly=false)
	public void delete(BizOrder bizOrder) {
		super.delete(bizOrder);
		BizOrderChild bizOrderChild = new BizOrderChild();
		bizOrderChild.setParent(bizOrder);
		bizOrderChildDao.delete(bizOrderChild);
	}
	
}